package com.ruoyi.controller;


import com.ruoyi.constants.Constant;
import com.ruoyi.dto.*;
import com.ruoyi.edu.domain.Risk;
import com.ruoyi.edu.service.IRiskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "风险评估")
@RequestMapping("/riskEvaluateWeb")
@RestController
public class RiskEvaluateWebController {

    @Autowired
    private IRiskService riskService;

    private static Map<String,String> dtDictMap = new HashMap<String,String>();//电梯模型
    private static Map<String,String> rqgwDictMap = new HashMap<String,String>();//燃气管网模型
    private static Map<String,String> gsgwDictMap = new HashMap<String,String>();//供水管网模型
    private static Map<String,String> rlgwDictMap = new HashMap<String,String>();//热力管网模型
    private static Map<String,String> qlDictMap = new HashMap<String,String>();//桥梁模型
    private static Map<String,String> jzxfDictMap = new HashMap<String,String>();//建筑消防模型
    private static Map<String,String> jtDictMap = new HashMap<String,String>();//交通模型
    static {
        /*--------------------电梯模型start------------------*/
        dtDictMap.put("yxnd","40~15~5#10#20");
        dtDictMap.put("dtjs","40~20~0#10#20");
        dtDictMap.put("szjz","40~30~10#20#30#5");
        dtDictMap.put("jzwc","40~20~5#10#20");
        dtDictMap.put("dtlx","40~5~10#5");
        dtDictMap.put("sfaz","60~20~20#5");
        dtDictMap.put("jlsy","60~10~5#15#30");
        dtDictMap.put("sfbj","60~30~0#10");
        dtDictMap.put("sfkr","60~20~0#30");
        dtDictMap.put("bjcz","60~20~10#30");
        /*--------------------电梯模型end------------------*/

        /*--------------------燃气管网模型start------------------*/
        //rqgwDictMap.put("gwjb_gl","20~30~5#10#20#30#15#5#10#20#5#15#20#");
        rqgwDictMap.put("gwjb_gl","20~10~5#10#20");
        rqgwDictMap.put("gwjb_gc","20~10~30#15#5#10");
        rqgwDictMap.put("gwjb_ms","20~5~20#5#15");
        rqgwDictMap.put("gwjb_gdss","20~5~20#0");
        rqgwDictMap.put("zrph_jscz","20~15~20#0");
        rqgwDictMap.put("zrph_gdzb","20~15~20#0");
        rqgwDictMap.put("rlph_gdsz","20~15~5#10#20");
        rqgwDictMap.put("rlph_gdsf","20~10~30#20#10#0");
        rqgwDictMap.put("rlph_gdfh","20~15~20#5#0");
        rqgwDictMap.put("zbwm","30~30~30#5");
        rqgwDictMap.put("yldj","30~10~30#15#5");
        rqgwDictMap.put("zbwb","30~30~5#10#30");
        rqgwDictMap.put("szlw","30~10~5#10");
        rqgwDictMap.put("szqy","30~10~5#10#15");
        rqgwDictMap.put("szqyjj","30~10~5#10#15");
        rqgwDictMap.put("jsnd","50~10~10#30#0");
        rqgwDictMap.put("xlhd","50~30~0#10");
        rqgwDictMap.put("zbrq","50~30~20#0");
        rqgwDictMap.put("bjcz","50~30~10#30");
        /*--------------------燃气管网模型end------------------*/

        /*--------------------供水管网模型start------------------*/
        gsgwDictMap.put("gwjb_gl","20~10~5#10#20");
        gsgwDictMap.put("gwjb_gc","20~10~5#15#5#30");
        gsgwDictMap.put("gwjb_ms","20~5~20#5#15");
        gsgwDictMap.put("gwjb_gdss","20~5~20#0");
        gsgwDictMap.put("zrph_jscz","20~15~20#0");
        gsgwDictMap.put("zrph_gdzb","20~15~20#0");
        gsgwDictMap.put("rlph_gdsz","20~15~5#10#20");
        gsgwDictMap.put("rlph_gdsf","20~10~30#20#10#0");
        gsgwDictMap.put("rlph_gdfh","20~15~20#5#0");
        gsgwDictMap.put("szlw","30~20~10#20");
        gsgwDictMap.put("szqy","30~10~0#5#10");
        gsgwDictMap.put("szqyjj","30~10~0#5#10");
        gsgwDictMap.put("gj","30~30~5#10#30");
        gsgwDictMap.put("jj","30~20~20#5");
        gsgwDictMap.put("zbsg","30~10~5#10#15");
        gsgwDictMap.put("jsnd","50~10~10#30#0");
        gsgwDictMap.put("xlhd","50~20~0#10");
        gsgwDictMap.put("zbyl","50~20~20#0");
        gsgwDictMap.put("gdsl","50~20~20#0");
        gsgwDictMap.put("bjcz","50~30~10#30");
        /*--------------------供水管网模型end------------------*/

        /*--------------------热力管网模型start------------------*/
        rlgwDictMap.put("gwjb_gl","20~10~5#10#20");
        rlgwDictMap.put("gwjb_ms","20~10~20#5#15");
        rlgwDictMap.put("gwjb_gdss","20~10~20#0");
        rlgwDictMap.put("zrph_jscz","20~15~20#0");
        rlgwDictMap.put("zrph_gdzb","20~15~20#0");
        rlgwDictMap.put("rlph_gdsz","20~15~5#10#20");
        rlgwDictMap.put("rlph_gdsf","20~10~30#20#10#0");
        rlgwDictMap.put("rlph_gdfh","20~15~20#5#0");
        rlgwDictMap.put("szlw","30~20~10#20");
        rlgwDictMap.put("szqy","30~10~0#5#10");
        rlgwDictMap.put("szqyjj","30~10~0#5#10");
        rlgwDictMap.put("gj","30~10~5#10#30");
        rlgwDictMap.put("yldj","30~20~5#10#30");
        rlgwDictMap.put("jj","30~20~20#0");
        rlgwDictMap.put("zbyb","30~30~5#10#30");
        rlgwDictMap.put("zbsg","30~10~5#10#15");
        rlgwDictMap.put("jsnd","50~10~10#30#0");
        rlgwDictMap.put("xlhd","50~30~0#10");
        rlgwDictMap.put("zbyl","50~30~20#0");
        rlgwDictMap.put("bjcz","50~30~10#30");
        /*--------------------热力管网模型end------------------*/

        /*--------------------桥梁模型start------------------*/
        qlDictMap.put("qxsf","20~25~30#0");
        qlDictMap.put("szdl","20~20~30#20#10#5");
        qlDictMap.put("zbsg","20~20~5#10#20");
        qlDictMap.put("yxnd","20~15~20#5#20");
        qlDictMap.put("jlzh","20~20~5#10#20");
        qlDictMap.put("qmcd","30~50~10#20");
        qlDictMap.put("qlsz","30~50~30#20#10#5");
        qlDictMap.put("sfaz","50~20~30#0");
        qlDictMap.put("dqjc","50~30~0#30");
        qlDictMap.put("bjcz","50~30~10#20");
        qlDictMap.put("qxyj","50~20~20#20");
        /*--------------------桥梁模型end------------------*/

        /*--------------------建筑消防模型start------------------*/
        jzxfDictMap.put("jzns","20~30~30#10");
        jzxfDictMap.put("jzwn","20~30~30#10");
        jzxfDictMap.put("hzyl","20~20~20#5");
        jzxfDictMap.put("lcsl","30~30~5#20#30");
        jzxfDictMap.put("jzwnc","30~30~5#10#20");
        jzxfDictMap.put("jzws","30~20~10#20#20#10#20");
        jzxfDictMap.put("sfjx","50~30~30#0");
        jzxfDictMap.put("dqsf","50~30~0#30");
        jzxfDictMap.put("bjsf","50~40~20#30");
        /*--------------------建筑消防模型end------------------*/

        /*--------------------交通模型start------------------*/
        jtDictMap.put("cllx","50~10~10#5#3#0");
        jtDictMap.put("yshx","50~20~30#20#10");
        jtDictMap.put("whps","50~30~30#20#10");
        jtDictMap.put("szlw","50~10~5#10");
        jtDictMap.put("clsz","50~30~30#20#0");
        jtDictMap.put("sfaz","50~20~30#10");
        jtDictMap.put("jksf","50~30~0#20");
        jtDictMap.put("bjcz","50~30~10#20");
        jtDictMap.put("qxzk","50~20~10#10#10#10#0");
        /*--------------------交通模型end------------------*/
    }


    @ApiOperation("DT-电梯")
    @PostMapping("/dt")
    public Map dt(@RequestBody DtDto dtDto) {
        Map map = new HashMap();
        map.put("number",dtDto.getNumber());

        Double score = Constant.DoubleFormat2(calculateScore(dtDto,dtDictMap,"DT"));
        map.put("score",score);
        map.put("name",this.riskLevel(score));
        return map;
    }



    @ApiOperation("RQGW-燃气管网")
    @PostMapping("/rqgw")
    public Map rqgw(@RequestBody RqgwDto rqgwDto){
        Map map = new HashMap();
        map.put("number",rqgwDto.getNumber());

        if(rqgwDto.getGwjb_gl()!=null&&rqgwDto.getGwjb_gl()!="")
        {
            Double gl = Double.parseDouble(rqgwDto.getGwjb_gl());
            if(gl<5){
                rqgwDto.setGwjb_gl("1");
            }else if(gl<15){
                rqgwDto.setGwjb_gl("2");
            }else{
                rqgwDto.setGwjb_gl("3");
            }
        }
        if(rqgwDto.getGwjb_ms()!=null && rqgwDto.getGwjb_ms()!="")
        {
            Double ms = Double.parseDouble(rqgwDto.getGwjb_ms());
            if(ms<0.8){
                rqgwDto.setGwjb_ms("1");
            }else if(ms<1.5){
                rqgwDto.setGwjb_ms("2");
            }else{
                rqgwDto.setGwjb_ms("3");
            }
        }

        Double score = Constant.DoubleFormat2(calculateScore(rqgwDto,rqgwDictMap,"RQ"));
        map.put("score",score);
        map.put("name",this.riskLevel(score));
        return map;
    }

    @ApiOperation("GSGW-供水管网")
    @PostMapping("/gsgw")
    public Map gsgw(@RequestBody GsgwDto gsgwDto) {
        Map map = new HashMap();
        map.put("number",gsgwDto.getNumber());

        if(gsgwDto.getGj() !=null)
        {
            Double gj = Double.parseDouble(gsgwDto.getGj());
            if(gj<200){
                gsgwDto.setGj("1");
            }else if(gj<600){
                gsgwDto.setGj("2");
            }else{
                gsgwDto.setGj("3");
            }
        }

        Double score = Constant.DoubleFormat2(calculateScore(gsgwDto,gsgwDictMap,"GS"));
        map.put("score",score);
        map.put("name",this.riskLevel(score));
        return map;
    }

    @ApiOperation("RLGW-热力管网")
    @PostMapping("/rlgw")
    public Map rlgw(@RequestBody RlgwDto rlgwDto) {
        Map map = new HashMap();
        map.put("number",rlgwDto.getNumber());

        if(rlgwDto.getGwjb_ms()!=null) {
            Double ms = Double.parseDouble(rlgwDto.getGwjb_ms());

            if (ms < 0.8) {
                rlgwDto.setGwjb_ms("1");
            } else if (ms < 1.5) {
                rlgwDto.setGwjb_ms("2");
            } else {
                rlgwDto.setGwjb_ms("3");
            }
        }
        if(rlgwDto.getGj()!=null) {
            Double gj = Double.parseDouble(rlgwDto.getGj());
            if (gj < 200) {
                rlgwDto.setGj("1");
            } else if (gj < 600) {
                rlgwDto.setGj("2");
            } else {
                rlgwDto.setGj("3");
            }
        }
        Double score = Constant.DoubleFormat2(calculateScore(rlgwDto,rlgwDictMap,"RL"));
        map.put("score",score);
        map.put("name",this.riskLevel(score));
        return map;
    }

    @ApiOperation("QL-桥梁")
    @PostMapping("/ql")
    public Map ql(@RequestBody QlDto qlDto){
        Map map = new HashMap();
        map.put("number",qlDto.getNumber());

        Double score = Constant.DoubleFormat2(calculateScore(qlDto,qlDictMap,"QL"));
        map.put("score",score);
        map.put("name",this.riskLevel(score));
        return map;
    }

    @ApiOperation("JZXF-建筑消防")
    @PostMapping("/jzxf")
    public Map jzxf(@RequestBody JzxfDto jzxfDto) {
        Map map = new HashMap();
        map.put("number",jzxfDto.getNumber());

        if (jzxfDto.getLcsl() != null) {
            Double lcsl = Double.parseDouble(jzxfDto.getLcsl());
            if (lcsl < 0.8) {
                jzxfDto.setLcsl("1");
            } else if (lcsl < 1.5) {
                jzxfDto.setLcsl("2");
            } else {
                jzxfDto.setLcsl("3");
            }
        }
        if (jzxfDto.getJzwnc() != null) {
            Double jzwnc = Double.parseDouble(jzxfDto.getJzwnc());
            if (jzwnc < 0.6) {
                jzxfDto.setJzwnc("3");
            } else if (jzwnc < 1) {
                jzxfDto.setJzwnc("2");
            } else {
                jzxfDto.setJzwnc("1");
            }
        }
        Double score = Constant.DoubleFormat2(calculateScore(jzxfDto,jzxfDictMap,"XF"));
        map.put("score",score);
        map.put("name",this.riskLevel(score));
        return map;
    }

    @ApiOperation("JT-交通")
    @PostMapping("/jt")
    public Map jt(@RequestBody JtDto jtDto) {
        Map map = new HashMap();
        map.put("number",jtDto.getNumber());
        Double score = Constant.DoubleFormat2(calculateScore(jtDto,jtDictMap,"JT"));
        map.put("score",score);
        map.put("name",this.riskLevel(score));
        return map;
    }

    private Double calculateScore(Object obj,Map<String,String> dictMap,String category){
        Risk where = new Risk();
        where.setCategory(category);
        where.setLevel(2l);
        List<Risk> riskList = riskService.selectRiskList(where);
        for(Risk risk:riskList){
            Risk riskP = riskService.selectRiskById(risk.getParentId());
            Double weight = risk.getWeight();
            String code = risk.getCode();
            Double weightP = riskP.getWeight();
            String dict = dictMap.get(code);
            dictMap.put(code,weightP.toString() + "~" + weight.toString() + "~" + dict.split("~")[2]);
        }
        return calculateScore(obj,dictMap);
    }

    private Double calculateScore(Object obj,Map<String,String> dictMap){
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        Double score = 0.0;
        for(Field field:fields){
            field.setAccessible(true);
            try {
                if(field.get(obj)==null){
                    continue;
                }
                String fieldName = field.getName();
                String fieldValue = field.get(obj).toString();
                //获取三级分数下标值，需要是整数，如1、2、3、4
                int index = Integer.parseInt(fieldValue);
                //获取字段对应字典数据
                String dictValue = dictMap.get(fieldName);
                //获取一二三级数据值
                String[] level = dictValue.split("~");
                //获取三级明细值
                String[] three = level[2].split("#");
                //统计三级明细总值
                Double threeCount = 0.0;
                for(String t:three){
                    threeCount+=Double.parseDouble(t);
                }
                //获取参数对应三级分数值
                String threeScore = three[index-1];
                //计算该字段对应分值：三级分数*二级占比*一级占比/100
                //计算该字段对应分值：三级分数/三級统计*二级占比*一级占比
                score += Constant.DoubleFormat((100*Double.parseDouble(threeScore)/threeCount*Double.parseDouble(level[1])*Double.parseDouble(level[0])));
            }catch (Exception e){

                e.printStackTrace();
            }
        }
        return score;
    }

    private String riskLevel(Double score){
        String level = "四级风险";
        if(score>=80){
            level = "一级风险";
        }else if(score>=50){
            level = "二级风险";
        }else if(score>=20){
            level = "三级风险";
        }else{
            level = "四级风险";
        }
        return level;
    }

    public static void main(String[] args) {
        JtDto jtDto = new JtDto();
        jtDto.setBjcz("1");
        jtDto.setCllx("2");
        jtDto.setQxzk("2");
        jtDto.setYshx("1");
        jtDto.setWhps("1");
        jtDto.setSzlw("1");
        jtDto.setClsz("1");
        jtDto.setSfaz("1");
        jtDto.setJksf("1");
        jtDto.setYshx("1");
        jtDto.setYshx("1");
        RiskEvaluateWebController risk = new RiskEvaluateWebController();
        Double score = risk.calculateScore(jtDto,jtDictMap,"DT");
        System.out.println(score);
    }
}
